package com.wulis.provider.authority.web;

import static com.wulis.common.model.enums.VersionEnum.WEB_V_1_0;

import java.util.List;

import javax.validation.Valid;

import com.wulis.common.model.enums.EnableStateEnum;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wulis.common.annotation.ApiVersion;
import com.wulis.common.model.dto.IdDto;
import com.wulis.common.support.BaseController;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import com.wulis.provider.authority.model.dto.InsertRoleDto;
import com.wulis.provider.authority.model.dto.QueryRolePageDto;
import com.wulis.provider.authority.model.dto.UpdateRoleDto;
import com.wulis.provider.authority.model.vo.QueryRoleListVo;
import com.wulis.provider.authority.model.vo.QueryRolePageVo;
import com.wulis.provider.authority.model.vo.QueryRoleVo;
import com.wulis.provider.authority.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author WuliBao
 * @date 2020-04-17
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("web/permission/role")
@Api(tags = "系统权限管理:角色管理")
public class RoleController extends BaseController {
    private final RoleService roleService;
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryRolePage")
    @ApiOperation("查询角色列表(分页)")
    @PreAuthorize("@api.hasPermission('web:permission:role:queryRolePage')")
    public ApiWrapper<IPage<QueryRolePageVo>> queryRolePage(@Valid QueryRolePageDto queryRolePageDto) {
        return ApiWrapMapper.success(roleService.queryRolePage(queryRolePageDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryRoleList")
    @ApiOperation("查询角色列表(全量)")
    @PreAuthorize("@api.hasPermission('web:permission:role:queryRoleList')")
    public ApiWrapper<List<QueryRoleListVo>> queryRoleList() {
        return ApiWrapMapper.success(roleService.queryRoleList(EnableStateEnum.IS_ENABLED.value()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("queryRole")
    @ApiOperation("查询角色")
    @PreAuthorize("@api.hasPermission('web:permission:role:queryRole')")
    public ApiWrapper<QueryRoleVo> queryRole(@Valid IdDto idDto) {
        return ApiWrapMapper.success(roleService.queryRole(idDto.getId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("insertRole")
    @ApiOperation("新增角色")
    @PreAuthorize("@api.hasPermission('web:permission:role:insertRole')")
    public ApiWrapper<Integer> insertRole(@Valid @RequestBody InsertRoleDto insertRoleDto) {
        return ApiWrapMapper.success(roleService.insertRole(insertRoleDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PutMapping("updateRole")
    @ApiOperation("修改角色")
    @PreAuthorize("@api.hasPermission('web:permission:role:updateRole')")
    public ApiWrapper<Integer> updateRole(@Valid @RequestBody UpdateRoleDto updateRoleDto) {
        return ApiWrapMapper.success(roleService.updateRole(updateRoleDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @DeleteMapping("deleteRole")
    @ApiOperation("删除角色")
    @PreAuthorize("@api.hasPermission('web:permission:role:deleteRole')")
    public ApiWrapper<Integer> deleteRole(@Valid @RequestBody IdDto idDto) {
        return ApiWrapMapper.success(roleService.deleteRole(idDto.getId()));
    }
}
